package com.march.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.march.common.CustomException;
import com.march.entry.Category;
import com.march.entry.Dish;
import com.march.entry.Setmeal;
import com.march.mapper.CategoryMapper;
import com.march.service.CategoryService;
import com.march.service.DishService;
import com.march.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @auther:
 * @create 10 -- 20:17
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;
    /*
    * 根据id来删除分类
    *
    * 删除之前要检查分类是否关联了菜品
    * */
    @Override
    public void remove(Long id) {

        //查询当前分类是否关联菜品
        LambdaQueryWrapper<Dish> dishlqw = new LambdaQueryWrapper<>();
        dishlqw.eq(Dish::getCategoryId,id);
        int count = (int) dishService.count(dishlqw);
        if(count>0){
            //抛出异常
             throw new CustomException("有关联菜品存在");
        }
        //查询当前分类是否关联套餐
        LambdaQueryWrapper<Setmeal> setmeallqw = new LambdaQueryWrapper<>();
        setmeallqw.eq(Setmeal::getCategoryId,id);
        int count1 = (int) setmealService.count(setmeallqw);
        if(count1>0){
            //抛出异常
            throw new CustomException("有关联套餐存在");
        }
        //正常删除分类
        this.removeById(id);
    }
}
